//
//  OrderManageViewController.m
//  SameWorld(merchant)
//
//  Created by 李小朋 on 2017/3/31.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "OrderManageViewController.h"
#import "OrderSortMenuView.h"
#import "OrderManageCell.h"
#import "OrderCalendarView.h"
#import "UserInfo.h"
#import "OrderModel.h"
#import "OrderCommodityModel.h"
@interface OrderManageViewController ()<UITableViewDelegate,UITableViewDataSource,OrderSortMenuViewDelegate>
@property (nonatomic ,strong) UserInfo *userInfo;
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) OrderSortMenuView *orderSortMenuView;
@property (nonatomic ,strong) OrderCalendarView *orderCanlendarView;
@property (nonatomic ,strong) OrderCommodityModel *commodityModel;
@property (nonatomic ,assign) NSInteger currentIndex;  // 记录当前选择的状态

@property (nonatomic ,strong) NSMutableArray *commodityArray;
@property (nonatomic ,strong) NSMutableArray *dataSource;
@end

@implementation OrderManageViewController

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
         _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (NSMutableArray *)commodityArray {
    
    if (!_commodityArray) {
        self.commodityArray = [NSMutableArray array];
    }
    return _commodityArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"订单管理";
    _userInfo = [UserInfo sharedUserInfo];
    [self loadDataOrderList];
    [self createOrderMenuViewUI];
    [self createTableViewUI];
    [self setNarBarUI];
    
    // 默认全部
    self.currentIndex = 9;

}

- (void)loadDataOrderList {

    
//  http://118.190.98.64/tctxAPI/shop_order_list.aspx?zt=1&page=1&shop_bh=1&page_count=8
    NSString *urlStr = [NSString stringWithFormat:@"%@/shop_order_list.aspx",serverUrl];
    NSDictionary *paramDic = @{@"shop_bh":_userInfo.bh,@"zt":@(_currentIndex),@"page":@"1",@"page_count":@"8"};
//    NSLog(@"%@ ++ ",@(_currentIndex));
    [NetworkManager requestPOSTWithURLStr:urlStr paramDic:paramDic finish:^(id responseObject) {
//        NSLog(@"请求成功%@",responseObject);
        [self.dataSource removeAllObjects];
        [self.commodityArray removeAllObjects];
        if (RESPONSEOBJECT) {
            NSArray *dataArr = [responseObject objectForKey:@"data"];
            for (NSDictionary *dict in dataArr) {
                OrderModel *model = [[OrderModel alloc] init];
                [model setValuesForKeysWithDictionary:dict];
                [self.dataSource addObject:model];
                
                // 商品信息
                NSDictionary *commodityDic = [dict objectForKey:@"data_commodity"];
                self.commodityModel = [OrderCommodityModel mj_objectWithKeyValues:commodityDic];
                [self.commodityArray addObject:self.commodityModel];
                
            }
        }else{
        
            [self showAlertWithTitle:responseObject[@"message"]];
        }
    
        [self.tableView reloadData];

    } enError:^(NSError *error) {
        NSLog(@"请求失败%@",error);
    }];
}


- (void)setNarBarUI {
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"rili"] style:(UIBarButtonItemStylePlain) target:self action:@selector(rightItemClicked:)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    
    
}

#pragma mark - 日历按钮
- (void)rightItemClicked:(UIBarButtonItem *)sender {

    NSLog(@"日历");
    OrderCalendarView *orderCanlendarView = [[OrderCalendarView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    orderCanlendarView.selectTime = ^(NSString *time) {
        NSLog(@"xxx");
    };
    [orderCanlendarView showCustomCalendarView];
    
}

- (void)createOrderMenuViewUI {
    
    NSArray *titleArray = @[@"全部",@"待支付",@"未使用",@"待评价",@"退款"];
    NSArray *imageArray = @[@"quanbuweidian",@"daizhifuweidian",@"weishiyongweidianzhong",@"daipingjiaweidian",@"tuikuanweidian"];
    NSArray *selectedImageArray = @[@"quanbudianhong",@"daizhifudianzhong",@"weishiyongdianzhong",@"daipingjiadianzhong",@"tuikuandianzhong"];
    self.orderSortMenuView = [[OrderSortMenuView alloc] initWithFrame:(CGRectMake(0, 0, size_width, 64)) withTitleArray:titleArray imageArray:imageArray selectedImageArray:selectedImageArray];
    self.orderSortMenuView.backgroundColor = [UIColor whiteColor];
    self.orderSortMenuView.delegate = self;
    [self.view addSubview:self.orderSortMenuView];
}

- (void)createTableViewUI {
    
    self.tableView = [[UITableView alloc] initWithFrame:(CGRectMake(0, 64, size_width, size_height-72-48)) style:(UITableViewStylePlain)];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"f2f2f2"];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    
    UIView *headerView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, size_width, 8))];
    headerView.backgroundColor = [UIColor colorWithHexString:@"f2f2f2"];
    self.tableView.tableHeaderView = headerView;
    
    // 注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"OrderManageCell" bundle:nil] forCellReuseIdentifier:@"OrderManageCell"];
    
}

#pragma mark 点击选中的菜单方法
- (void)buttonClickWithTag:(NSInteger )index {
    
    switch (index) {
        case 800:
        {
             [self loadDataOrderList];
            _currentIndex = 9;
            
        }
            break;
        case 801:
        {
             [self loadDataOrderList];
            _currentIndex = 0;
         
        }
            break;
        case 802:
        {
             [self loadDataOrderList];
            _currentIndex = 1;

        }
            break;
        case 803:
        {
            [self loadDataOrderList];
            _currentIndex = 2;
        }
            break;
        case 804:
        {
            [self loadDataOrderList];
            _currentIndex = 3;
        }
            break;
        default:
            break;
    }
    
    [self.tableView reloadData];
}

#pragma mark - UITableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    OrderManageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderManageCell" forIndexPath:indexPath];

    OrderModel *model = [self.dataSource objectAtIndex:indexPath.section];
    OrderCommodityModel *commodityModel = [self.commodityArray objectAtIndex:indexPath.section];
    [cell setUpDataWithOrderModel:model withType:[NSString stringWithFormat:@"%ld", _currentIndex]];
    [cell setUpDataWtihrderCommodityModel:commodityModel];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 222;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 8;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
}


@end
